package winOrDie.graphicsInterface.GameMenu;

import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Vector;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import winOrDie.Application.WoD_Application_Data;
import winOrDie.Configuration.WoD_Configuration;
import winOrDie.Net.Client.WoD_Peer;
import winOrDie.Net.Http.httpmsg.WoD_HttpMessage;
import winOrDie.Net.Http.httpmsg.WoD_InfoPartidasActuales;

public class Composite_List_Games extends Composite {

	private Composite composite_Header = null;
	private Label label_Games_List = null;
	
	private Composite composite_List = null;
	private Table list_Games = null;
	
	private Composite composite_Options = null;
	private Button button_Cancel = null;
	private Button button_Refresh = null;
	
	private String text_Peer_Host = null;
	private String text_Peer_Port = null;

	public String getText_Peer_Host() {
		return text_Peer_Host;
	}

	public void setText_Peer_Host(String text_Peer_Host) {
		this.text_Peer_Host = text_Peer_Host;
	}

	public String getText_Peer_Port() {
		return text_Peer_Port;
	}

	public void setText_Peer_Port(String text_Peer_Port) {
		this.text_Peer_Port = text_Peer_Port;
	}

	private Vector<Composite_List_Games_Listener> listeners = null;
	
	public synchronized void add_Composite_List_Games_Listener(Composite_List_Games_Listener listener)
	{
		listeners.add(listener);
	}
	
	public synchronized void remove_Composite_List_Games_Listener(Composite_List_Games_Listener listener)
	{
		listeners.remove(listener);
	}
	
	protected void fire_Composite_List_Games_Event(Composite_List_Games_Event event)
	{
		for (Composite_List_Games_Listener listener : listeners) {
			listener.on_Composite_List_Games_Changed(event);
		}
	}
	
	public Composite_List_Games(Composite parent, int style) {
		super(parent, style);
		
		listeners = new Vector<Composite_List_Games_Listener>();
		initialize();
//		Composite_List_Games_Event event = new Composite_List_Games_Event(this);
//		event.setEventType(Composite_List_Games_Event.Event_Types.LIST_GAMES);
//		fire_Composite_List_Games_Event(event);
	}

	private void initialize() {
		this.setSize(new Point(331, 173));
		setLayout(new GridLayout());
				
		get_Header();
		get_Games_List();
	
		
//		
		get_Composite_Options();
		
		
//		final Composite_List_Games compositeListGames = this;
//		Composite_List_Games_Event event = new Composite_List_Games_Event(compositeListGames);
//		event.setEventType(Composite_List_Games_Event.Event_Types.LIST_GAMES);
//		fire_Composite_List_Games_Event(event);
	}
	
	
	private Composite get_Header()
	{
		if (composite_Header == null)
		{
			GridLayout gridLayout_Composite_Header = new GridLayout();
			composite_Header = new Composite(this, SWT.NONE);
			composite_Header.setLayout(gridLayout_Composite_Header);
			
			
			label_Games_List = new Label(composite_Header, SWT.NONE);
			label_Games_List.setText("Lista de partidas");
		}
		
		return composite_Header;
	}
	
	private Table get_Games_List()
	{
		if (list_Games == null) {
			
			GridData gridData_List_Games = new GridData();
			gridData_List_Games.grabExcessHorizontalSpace = true;
			gridData_List_Games.horizontalAlignment = SWT.FILL;
			gridData_List_Games.grabExcessVerticalSpace = true;
			gridData_List_Games.verticalAlignment = SWT.FILL;
			
			
			list_Games = new Table(this, SWT.NONE);
			list_Games.setHeaderVisible(true);
			list_Games.setLinesVisible(true);

			//list_Games.setBounds(10, 10, 300, 300);
			list_Games.setLayoutData(gridData_List_Games);
			
			for (int i=0; i<6; i++) {
				TableColumn column = new TableColumn (list_Games, SWT.NONE);
				
				if(i==0) {column.setText ("Id partida"); column.setWidth(40);}
				if(i==1) {column.setText ("Nombre de partida"); column.setWidth(50);}
				if(i==2) {column.setText ("Creador de partida"); column.setWidth(50);}
				if(i==3) {column.setText ("IP de partida"); column.setWidth(50);}
				if(i==4) {column.setText ("Puerto de partida"); column.setWidth(60);}
				if(i==5) {column.setText ("Jugadores actuales"); column.setWidth(60);}			
			}
						
			list_Games.addListener(SWT.DefaultSelection, new Listener() {
                public void handleEvent (Event e) {
                	Composite_List_Games_Event event = new Composite_List_Games_Event(this);
					event.setEventType(Composite_List_Games_Event.Event_Types.CONNECT_TO_GAME);
					
					
					for(int i = 0; i<list_Games.getItems().length; i++)
					{
						TableItem item = list_Games.getItem(i);
						
						System.out.println("\n GETTEXT(0): " + item.getText(0).toString());
						System.out.println("\n GETTEXT(1): " + item.getText(1).toString());
						System.out.println("\n GETTEXT(2): " + item.getText(2).toString());
						System.out.println("\n GETTEXT(3): " + item.getText(3).toString());
						System.out.println("\n GETTEXT(4): " + item.getText(4).toString());
						System.out.println("\n GETTEXT(5): " + item.getText(5).toString());
						
						System.out.println("\n COMPARACIO DE "+ e.item.toString() + " i " + item.getText(0).toString() + " = " + String.valueOf(e.item.toString().equals(item.getText(0))));
						
						String e_string = e.item.toString().replace("TableItem {", "");
						e_string = e_string.replace("}", "");
						
						if(e_string.equals(item.getText(0)))
						{
							System.out.println("\n PEER HOST: " + item.getText(3));
							System.out.println("\n PEER PORT+: " + item.getText(4));
							
							setText_Peer_Host(item.getText(3));
							setText_Peer_Port(item.getText(4));
						}
					}
					
					System.out.println("default "+e.item);
					fire_Composite_List_Games_Event(event);
					
                }
			});

		}
		return list_Games;
	}
	

	
	private Composite get_Composite_Options()
	{
		if (composite_Options == null)
		{
			RowLayout gridLayout_Composite_Options = new RowLayout();
			GridData gridData_Composite_Options = new GridData();
			gridData_Composite_Options.grabExcessHorizontalSpace = true;
			gridData_Composite_Options.horizontalAlignment = SWT.FILL;
			
			composite_Options = new Composite(this, SWT.NONE);
			composite_Options.setLayout(gridLayout_Composite_Options);
			composite_Options.setLayoutData(gridData_Composite_Options);
			
			/**
			 * Button Cancel
			 */
			button_Cancel = new Button(composite_Options, SWT.NONE);
			button_Cancel.setText("Cancelar");
			
			final Composite_List_Games compositeListGames = this;
			button_Cancel.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					Composite_List_Games_Event event = new Composite_List_Games_Event(compositeListGames);
					event.setEventType(Composite_List_Games_Event.Event_Types.CANCEL);
					fire_Composite_List_Games_Event(event);
				}
			});
			
			/**
			 * Button Refresh
			 */
			button_Refresh = new Button(composite_Options, SWT.NONE);
			button_Refresh.setText("Refrescar");
			
			//final Composite_List_Games compositeListGames2 = this;
			button_Refresh.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					//THIS IS A ÑAPA
					//Paint_Table(WoD_HttpMessage.GetCurrentGames(wod_configuration.getWeb_Server(), wod_configuration.getWeb_Server_Port()));
					Composite_List_Games_Event event = new Composite_List_Games_Event(compositeListGames);
					event.setEventType(Composite_List_Games_Event.Event_Types.LIST_GAMES);
					fire_Composite_List_Games_Event(event);
				}
			});
			
		}
		
		return composite_Options;
	}

	public void Paint_Table(final ArrayList<WoD_InfoPartidasActuales> wodipa)
	{
		Runnable runnable = new Runnable() {
		
		@Override
		public void run() {
			
			getDisplay().syncExec(new Runnable() {
				
				@Override
				public void run() {
					list_Games.removeAll();
				}
			});
			
							
			for (final WoD_InfoPartidasActuales woDPartida : wodipa) {
				
				getDisplay().syncExec(new Runnable() {
					
					@Override
					public void run() {
						TableItem item = new TableItem(list_Games, SWT.NONE);
						final TableColumn [] columns = list_Games.getColumns();

						for (int i = 0; i<columns.length; i++)
						{
							System.out.println("\n COLUMN NAME: " + columns[i].getText());
									
							item.setText(i++ ,woDPartida.getGameID());
							item.setText(i++ ,woDPartida.getGameTitle());
							item.setText(i++ ,woDPartida.getServerName());
							item.setText(i++ ,woDPartida.getServerIP());
							item.setText(i++ ,woDPartida.getServerPort());
							item.setText(i++ ,woDPartida.getPlayersNumber());		

						}
					}
				});				
			}
			
		}
	};
	
	new Thread(runnable).start(); 		
	

	}


}  //  @jve:decl-index=0:visual-constraint="10,10"
